//
//  NYSView.m
//  NYS
//
//  Created by niyongsheng.github.io on 2021/8/9.
//  Copyright © 2021 NYS. ALL rights reserved.
//

#import "NYSExcDescView.h"
#import "MerchantCell.h"

@interface NYSExcDescView ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) UIView *view;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArr;
@property (nonatomic, strong) UIView *bgView;
@end

@implementation NYSExcDescView

- (instancetype)initWithFrame:(CGRect)frame upcode:(NSString *)code{
    self = [super initWithFrame:frame];
    self.code = code;
    [self setupView:@"1"];
    return self;
}



- (void)setupView:(NSString *)puFaType {
//    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
//    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
//    maskLayer.frame = self.frame;
//    maskLayer.path = path.CGPath;
//    maskLayer.lineWidth = 0;
//    maskLayer.strokeColor = UIColor.clearColor.CGColor;
//    self.layer.mask = maskLayer;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - SCREEN_HEIGHT*0.7, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.layer.masksToBounds = YES;
    self.tableView.layer.cornerRadius = 10;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.dataSourceArr = [NSMutableArray array];
    [self addSubview:self.bgView];
    NSDictionary *dic = @{@"partyCode":[UserDefaultUtil valueForKey:DriverPartyCode],@"bankCardCode":self.code};
    WEAKSELF
    [HTTPManager getNetworkRequest:[NSString stringWithFormat:@"%@/ntocc-basic-api/merchant/list", [ReAddressDynamic shared].baseUrl] withParameter:dic withVC:[UIViewController new] netName:@"快钱商户列表" success:^(id response, NSString *msg) {
        weakSelf.dataSourceArr = [NSMutableArray arrayWithArray:[MerchantModel mj_objectArrayWithKeyValuesArray:response]];
        [weakSelf.tableView reloadData];

    } failure:^(NSInteger code, NSString *msg) {

    }];
    
}
- (UIView *)bgView{
    if (!_bgView) {
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT + 100)];
        self.bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        [self.bgView addSubview:self.tableView];
    }
    return _bgView;
}


- (void)updateData:(id)data {

}

#pragma mark - tableview delegate / dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 96;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"MerchantCell";
    MerchantCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MerchantCell" owner:self options:nil] firstObject];
    }
    cell.model = self.dataSourceArr[indexPath.row];
    MerchantModel *modelm = self.dataSourceArr[indexPath.row];
    if ([modelm.accountType intValue] == 0) {
        if ((int)self.bindCardStatus == 3) {
            [cell.typeButton setTitle:@"已绑" forState:(UIControlStateNormal)];
        }else{
            [cell.typeButton setTitle:@"绑卡" forState:(UIControlStateNormal)];
        }
    }else if ([modelm.accountType intValue] == 1) {
        if ([self.hatBindCardStatus intValue] == 3) {
            [cell.typeButton setTitle:@"已绑" forState:(UIControlStateNormal)];
        }else{
            [cell.typeButton setTitle:@"绑卡" forState:(UIControlStateNormal)];
        }
    }else{
        if ([modelm.ifBind intValue] == 1) {
            [cell.typeButton setTitle:@"已绑" forState:(UIControlStateNormal)];
        }else{
            [cell.typeButton setTitle:@"绑卡" forState:(UIControlStateNormal)];
        }
    }
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *titLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 100, 44)];
    titLabel.text = @"请选择商户";
    titLabel.font = [UIFont systemFontOfSize:18];
    UIButton *button = [UIButton  buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(SCREEN_WIDTH - 60, 0, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"gas_icon_fail"] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview: titLabel];
    [view addSubview:button];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MerchantModel *model = self.dataSourceArr[indexPath.row];
//    if ([model.accountType intValue] == 0) {
//        if ((int)self.bindCardStatus == 3) {
//            self.returnBlock(@"浦发商户已绑定");
//            return;
//        }
//    }
//    if ([model.accountType intValue] == 1) {
//        if ([self.hatBindCardStatus intValue] == 3) {
//            self.returnBlock(@"快钱商户已绑定");
//            return;
//        }
//    }
   
    if (_block) {
        self.block(self.code, model.code,model.accountType);
    }
}

- (void)buttonAction{
    if (_block) {
        self.block(self.code, nil, nil);
    }
}
@end
